package cn.iocoder.yudao.module.biz.service.expand.record;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.expand.record.vo.*;
import cn.iocoder.yudao.module.biz.service.bizsalesteam.BizSalesTeamService;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.user.AdminUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.format.DateTimeFormatter;
import java.util.*;

import cn.iocoder.yudao.module.biz.dal.dataobject.expand.record.ExpandRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.expand.record.ExpandRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 拓展记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ExpandRecordServiceImpl implements ExpandRecordService {

    @Resource
    private AdminUserServiceImpl adminUserService;

    @Resource
    private ExpandRecordMapper expandRecordMapper;

    @Autowired
    private BizSalesTeamService salesTeamService;



    @Override
    public Long createExpandRecord(ExpandRecordSaveReqVO createReqVO) {
        // 插入
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        createReqVO.setOwnerUserId(loginUser.getId());
        createReqVO.setDeptId(Long.parseLong(loginUser.getInfo().get("deptId")));
        ExpandRecordDO expandRecord = BeanUtils.toBean(createReqVO, ExpandRecordDO.class);
        expandRecordMapper.insert(expandRecord);

        // 返回
        return expandRecord.getId();
    }

    @Override
    public void updateExpandRecord(ExpandRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateExpandRecordExists(updateReqVO.getId());
        // 更新
        ExpandRecordDO updateObj = BeanUtils.toBean(updateReqVO, ExpandRecordDO.class);
        expandRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteExpandRecord(Long id) {
        // 校验存在
        validateExpandRecordExists(id);
        // 删除
        expandRecordMapper.deleteById(id);
    }

    @Override
        public void deleteExpandRecordListByIds(List<Long> ids) {
        // 删除
        expandRecordMapper.deleteByIds(ids);
        }


    private void validateExpandRecordExists(Long id) {
        if (expandRecordMapper.selectById(id) == null) {
            throw exception(BIZ_EXPAND_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ExpandRecordDO getExpandRecord(Long id) {
        return expandRecordMapper.selectById(id);
    }

    @Override
    public PageResult<ExpandRecordDO> getExpandRecordPage(ExpandRecordPageReqVO pageReqVO) {
        return expandRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public ExpandRecordCountRespVO countByType(int type, Long userId) {
        ExpandRecordCountReqVO reqVO = new ExpandRecordCountReqVO();
        //0全部, 1个人, 2小组
        if(0 != type){
            List<Long> userIdList = new ArrayList<>();
            if(1 == type){
                userIdList.add(userId);
            }
            if(2 == type){
                //根据userId查询所属的小组成员id集合

            }
            reqVO.setOwnerUserIds(userIdList);
        }
        return expandRecordMapper.countByType(reqVO);
    }

    @Override
    public List<ExpandRecordUserCountRespVO> countAllUserExpand(Long teamId, String month) {
        List<ExpandRecordUserCountRespVO> list = new ArrayList<>();
//        List<Long> list1 = new ArrayList<>();
//        list1.add(SecurityFrameworkUtils.getLoginUserDeptId());

//        UserPageReqVO reqVO = new UserPageReqVO();
//        reqVO.setPageNo(1);
//        reqVO.setPageSize(20);
//        PageResult<AdminUserDO> listUser = adminUserService.getUserPage(reqVO);
        List<AdminUserDO> users = salesTeamService.getUsers(teamId);
        for (AdminUserDO a: users
             ) {
            ExpandRecordCountRespVO e =  expandRecordMapper.countSumByIdAndMonth(a.getId(),month);
            ExpandRecordUserCountRespVO respVO = new ExpandRecordUserCountRespVO();
            respVO.setExpandRecordCountRespVO(e);
            respVO.setUser(a);
            list.add(respVO);
        }
        return list;
    }

    @Override
    public List<ExpandRecordRespVO> getExpandRecordCurrent(Long id,String month) {
        List<ExpandRecordRespVO>  list = expandRecordMapper.getExpandRecordCurrent(id,month);
        DateTimeFormatter simpleDateFormat = DateTimeFormatter.ofPattern("MM-dd");
        list.forEach(person -> {
            person.setCreateTimeStr(person.getCreateTime().format(simpleDateFormat));
            String status = "客户维护";
            String[] array = {"02_03","03_05","04_01","04_02"};
            /*if(Arrays.asList(array).contains(person.getExpandProcess())){
                status = "本月签约";
            } else if(person.getExpandProcess().startsWith("01")) {
                status = "拓展中";
            }*/
            person.setExpandStatus(status);
        });
        return list;
    }

    @Override
    public Map<String,Object> getExpandRecordGk() {
        return expandRecordMapper.getExpandRecordGk(SecurityFrameworkUtils.getLoginUser().getTenantId());
    }

    @Override
    public List<ExpandRecordDO> getListByCondition(ExpandRecordPageReqVO pageReqVO) {
        List<ExpandRecordDO> dataList = expandRecordMapper.selectListByCondition(pageReqVO);
        return dataList;
    }

    @Override
    public List<ExpandRecordDO> getListByInterviewee(ExpandRecordPageReqVO pageReqVO) {
        return expandRecordMapper.selectListByInterviewee(pageReqVO);
    }

    @Override
    public List<ExpandRecordCheckRespVo> checkRecordByCustomerName(String customerName) {
        return expandRecordMapper.checkRecordByCustomerName(customerName);
    }
}
